/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现TinyToolKit设置的窗口
* CreateTime：2020-07-07 14:56:32
* Version：1.0
* Modify Recorder：
*************************************************/

using TinyToolKit;
using UnityEngine;
using UnityEditor;

namespace TinyToolKitEditor
{
    public enum MenuMode
    {
        Manager,
        Editor
    }

    public class TinyToolWindow : EditorWindow
    {
        public static TinyToolWindow _instance;

        private MenuMode _menuMode;
        private string[] _managerMenuNames;
        private string[] _editorMenuNames;
        private int _selectedManagerMenuId;
        private int _selectedEditorMenuId;
        private float _leftAreaWidth;
        private GUIStyle _titleStyle;

        private DrawWindowBase _uiWindow;
        private DrawWindowBase _timerWindow;
        private DrawWindowBase _soundWindow;
        private DrawWindowBase _fsmWindow;
        private DrawWindowBase _objectPoolWindow;
        private DrawWindowBase _resLoadWindow;
        private DrawWindowBase _assetBundleWindow;
        private DrawWindowBase _localizationWindow;
        private DrawWindowBase _setupWindow;
        private DrawWindowBase _constTableWindow;
        private DrawWindowBase _lanTransWindow;
        private DrawWindowBase _uiTableWindow;
        private DrawWindowBase _abBuildWindow;
        private DrawWindowBase _dataNodeWindow;

        private TinyToolSettingAsset _tinyToolSetting;
        private TinyToolEditorSettingAsset _editorSetting;

        public static void ShowWindow()
        {
            _instance = GetWindow<TinyToolWindow>();
            _instance.titleContent = new GUIContent("TinyToolKit");
            _instance.Show();
        }

        private void OnEnable()
        {
            _managerMenuNames = new[] { "设置" };
            _editorMenuNames = new[] { "设置", "常量列表", "UI 配置表", "多国语言", "资源打包" };

            _leftAreaWidth = 150;

            _titleStyle = new GUIStyle
            {
                fontSize = 26, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleCenter,
                normal = new GUIStyleState { textColor = EditorGUIUtility.isProSkin ? Color.gray : Color.black }
            };

            InitEditorSetting();
            InitTinyToolSetting();
        }

        private void OnGUI()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.Width(position.width));
            _menuMode = (MenuMode)EditorGUILayout.EnumPopup(_menuMode, EditorStyles.toolbarDropDown, GUILayout.Width(150));
            GUILayout.FlexibleSpace();
            GUILayout.Label(System.DateTime.Now.ToLongTimeString());
            GUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();

            #region Left Menu

            EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.MaxWidth(_leftAreaWidth), GUILayout.Height(position.height));

            if (_menuMode == MenuMode.Manager)
                _selectedManagerMenuId = GetSelectedMenu(_selectedManagerMenuId, _managerMenuNames);
            else if (_menuMode == MenuMode.Editor)
                _selectedEditorMenuId = GetSelectedMenu(_selectedEditorMenuId, _editorMenuNames);

            EditorGUILayout.EndVertical();

            #endregion

            #region Right Content

            EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.MaxWidth(position.width - _leftAreaWidth), GUILayout.MaxHeight(position.height));

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Tiny Tool Kit", _titleStyle);
            EditorGUILayout.Space();
            //EditorGUILayout.LabelField("", new GUIStyle("EyeDropperHorizontalLine"));
            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            if (_menuMode == MenuMode.Manager)
                DrawManagerGUI(_selectedManagerMenuId);
            else if (_menuMode == MenuMode.Editor)
                DrawEditorGUI(_selectedEditorMenuId);

            EditorGUILayout.EndVertical();

            #endregion

            EditorGUILayout.EndHorizontal();

            Repaint();
        }

        private int GetSelectedMenu(int selectedId, string[] menus)
        {
            for (int i = 0; i < menus.Length; i++)
            {
                if (i > 0)
                    EditorGUILayout.BeginHorizontal(new GUIStyle("EyeDropperHorizontalLine"));
                GUILayout.Label(menus[i], new GUIStyle("Label") { fixedHeight = 25, alignment = TextAnchor.MiddleCenter });
                if (i > 0)
                    EditorGUILayout.EndHorizontal();

                Rect rect = GUILayoutUtility.GetLastRect();
                if (selectedId == i)
                    GUI.Box(rect, "", new GUIStyle("SelectionRect"));

                if (rect.Contains(Event.current.mousePosition) && Event.current.type == EventType.MouseDown)
                {
                    selectedId = i;
                    Event.current.Use();
                }
            }

            return selectedId;
        }

        private void DrawManagerGUI(int menuId)
        {
            if (menuId == 0) DrawManagerBaseSettingGUI();

            if (_tinyToolSetting == null) return;
            DrawWindowBase window = null;
            switch (menuId)
            {
                case 1:
                    window = _soundWindow;
                    break;
                case 2:
                    window = _uiWindow;
                    break;
                case 3:
                    window = _timerWindow;
                    break;
                case 4:
                    window = _dataNodeWindow;
                    break;
                case 5:
                    window = _fsmWindow;
                    break;
                case 6:
                    window = _objectPoolWindow;
                    break;
                case 7:
                    window = _resLoadWindow;
                    break;
                case 8:
                    window = _assetBundleWindow;
                    break;
                case 9:
                    window = _localizationWindow;
                    break;
            }

            EditorGUI.BeginChangeCheck();
            window?.DrawMainGUI();
            if (EditorGUI.EndChangeCheck())
                EditorUtility.SetDirty(_tinyToolSetting);
        }

        private void DrawEditorGUI(int menuId)
        {
            if (_editorSetting == null)
            {
                if (TinyToolWindowUtil.DrawButton("Setup TinyTool"))
                {
                    _editorSetting = TinyToolEditorSettingAsset.CreateAsset();
                    InitEditorSetting();
                }
            }

            if (_editorSetting == null) return;
            EditorGUI.BeginChangeCheck();

            switch (menuId)
            {
                case 0:
                    _setupWindow.DrawMainGUI();
                    break;
                case 1:
                    _constTableWindow.DrawMainGUI();
                    break;
                case 2:
                    _uiTableWindow.DrawMainGUI();
                    break;
                case 3:
                    _lanTransWindow.DrawMainGUI();
                    break;
                case 4:
                    _abBuildWindow.DrawMainGUI();
                    break;
            }

            if (EditorGUI.EndChangeCheck())
                EditorUtility.SetDirty(_editorSetting);
        }

        private void DrawManagerBaseSettingGUI()
        {
            EditorGUI.BeginDisabledGroup(true);
            _tinyToolSetting = (TinyToolSettingAsset)EditorGUILayout.ObjectField("TinyTool 配置：", _tinyToolSetting, typeof(TinyToolSettingAsset), false, GUILayout.MaxWidth(400));
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.Space();
            if (_tinyToolSetting != null)
            {
                EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
                _tinyToolSetting.persist = EditorGUILayout.Toggle("持久化：", _tinyToolSetting.persist);
                EditorGUI.EndDisabledGroup();
            }
        }

        private void InitTinyToolSetting()
        {
            if (_tinyToolSetting == null)
                _tinyToolSetting = Resources.Load<TinyToolSettingAsset>("TinyToolSetting");
            if (_tinyToolSetting == null) return;

            _managerMenuNames = new[] { "设置", "音效", "UI界面", "计时器", "数据结点", "有限状态机", "对象池", "Res资源", "AB资源", "本地化" };

            _uiWindow = new DrawWindowUI();
            _timerWindow = new DrawWindowTimer();
            _soundWindow = new DrawWindowSound();
            _fsmWindow = new DrawWindowFSM();
            _objectPoolWindow = new DrawWindowObjectPool();
            _resLoadWindow = new DrawWindowResLoad();
            _assetBundleWindow = new DrawWindowAssetbundle();
            _localizationWindow = new DrawWindowLocalization();
            _dataNodeWindow = new DrawWindowDataNode();

            _uiWindow.InitWindow(_tinyToolSetting);
            _timerWindow.InitWindow(_tinyToolSetting);
            _soundWindow.InitWindow(_tinyToolSetting);
            _fsmWindow.InitWindow(_tinyToolSetting);
            _objectPoolWindow.InitWindow(_tinyToolSetting);
            _resLoadWindow.InitWindow(_tinyToolSetting);
            _assetBundleWindow.InitWindow(_tinyToolSetting);
            _localizationWindow.InitWindow(_tinyToolSetting);
            _dataNodeWindow.InitWindow(_tinyToolSetting);
        }

        private void InitEditorSetting()
        {
            if (_editorSetting == null)
                _editorSetting = Resources.Load<TinyToolEditorSettingAsset>("TinyToolEditorSetting");
            if (_editorSetting == null) return;

            _setupWindow = new DrawWindowSetup();
            _constTableWindow = new DrawWindowConstTable();
            _lanTransWindow = new DrawWindowLanTrans();
            _uiTableWindow = new DrawWindowUITable();
            _abBuildWindow = new DrawWindowABBuild();

            _setupWindow.InitWindow(_editorSetting);
            _constTableWindow.InitWindow(_editorSetting);
            _lanTransWindow.InitWindow(_editorSetting);
            _uiTableWindow.InitWindow(_editorSetting);
            _abBuildWindow.InitWindow(_editorSetting);

            ((DrawWindowSetup)_setupWindow).CheckScriptingDefineSymbol(BuildTargetGroup.Standalone);
        }
    }
}